﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using EStore.Core.Catalogs;
using Moq;
using EStore.Infrastructure;
using EStore.Core.Customers;

namespace EStore.UnitTests.EStore.Core
{
    public class WithRightRepository
    {
        static WithRightRepository()
        {
            //产品
            var productRepositoryMock = new Mock<IProductRepository>();
            productRepositoryMock.Setup(p => p.FindByCode(
                It.Is<string>(code => !string.IsNullOrEmpty(code) && Datas.Products.Any(s => s.Code.Equals(code))))).Returns((string code) =>
                {
                    return Datas.Products.SingleOrDefault(pp => pp.Code.Equals(code, StringComparison.OrdinalIgnoreCase));
                });
            Project.Util.EngineContext.Current.ContainerManager.AddComponentInstance<IProductRepository>(productRepositoryMock.Object);

            //顾客
            var customerRepositoryMock = new Mock<ICustomerRepository>();
            customerRepositoryMock.Setup(p => p.FindByUserName(
               It.Is<string>(name => !string.IsNullOrEmpty(name) && Datas.Customers.Any(s => s.UserName.Equals(name))))).Returns((string code) =>
               {
                   return Datas.Customers.SingleOrDefault(pp => pp.UserName.Equals(code, StringComparison.OrdinalIgnoreCase));
               });
            Project.Util.EngineContext.Current.ContainerManager.AddComponentInstance<ICustomerRepository>(customerRepositoryMock.Object);
        }

        protected class Datas
        {
            internal static IList<Product> Products = new List<Product>
              {
                 new Product{  Code="111111", Name ="自由女神",SalePrice=28m,SKU=18},
                 new Product{  Code="111112", Name ="C++入门经典", SalePrice=98m,SKU=3},
                 new Product{  Code="111115", Name ="Apple", SalePrice=5534.8m,SKU=2008},
                 new Product{  Code="111116", Name ="轮滑鞋", SalePrice=428.7m,SKU=118},
                 new Product{  Code="111117", Name ="IPad", SalePrice=3104.97m,SKU=1118},
              };

            internal static IList<Customer> Customers = new List<Customer>
            {
              new Customer{ UserName ="test@126.com"},
              new Customer{ UserName ="test1@126.com"},
              new Customer{ UserName ="test2@126.com"}
            };
        }
    }
}
